package com.docmanage.am.service.impl;

import com.docmanage.am.domain.*;
import com.docmanage.am.domain.dto.FileDTO;
import com.docmanage.am.domain.vo.FileVO;
import com.docmanage.am.mapper.AmArchiveMapper;
import com.docmanage.am.mapper.AmCabinetMapper;
import com.docmanage.am.mapper.AmFolderMapper;
import com.docmanage.am.common.constant.CommonConstants;
import com.docmanage.common.utils.DateUtils;
import com.docmanage.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 档案文件Service业务层处理
 * 
 * @author pointLion
 * @date 2023-06-18
 */
@Service
public class AmArchiveService
{
    @Resource
    private AmArchiveMapper amArchiveMapper;
    @Resource
    private AmFolderMapper amFolderMapper;
    @Resource
    private AmCabinetMapper amCabinetMapper;
    @Resource
    private AmBrowseService amBrowseService;
    /**
     * 查询档案文件
     * 
     * @param id 档案文件主键
     * @return 档案文件
     */
    public AmArchive selectAmArchiveById(Long id)
    {
        AmArchive amArchive = amArchiveMapper.selectAmArchiveById(id);
        return amArchive;
    }

    /**
     * 查询档案文件列表
     * 
     * @param amArchive 档案文件
     * @return 档案文件
     */
    public List<AmArchive> selectAmArchiveList(AmArchive amArchive)
    {
        //租户ID
        //amArchive.setTenantId(SecurityUtils.getTenantId());
        return amArchiveMapper.selectAmArchiveList(amArchive);
    }

    /**
 * 新增档案文件
 *
 * @param amArchive 档案文件
 * @return 结果
 */
public int insertAmArchive(AmArchive amArchive)
{
    //生成订单编号
    String billCode = UUID.randomUUID().toString().replaceAll("-", "");//serialService.generateBillCodeByBillType(BillTypeConstants.ARCHIVE_UPLOAD);
    amArchive.setCreateTime(DateUtils.getNowDate());
    amArchive.setCode(billCode);
    amArchive.setCreateBy(SecurityUtils.getUsername());
    amArchive.setDeptId(SecurityUtils.getDeptId());
    //租户ID
    //amArchive.setTenantId(SecurityUtils.getTenantId());
    amArchive.setCreateTime(DateUtils.getNowDate());
    return amArchiveMapper.insertAmArchive(amArchive);
}


    /**
     * 新增档案文件
     *
     * @param amDescription 档案数据
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    public int insertAmArchiveList(AmDescription amDescription)
    {
        AtomicReference<Integer> i= new AtomicReference<>(0);
        amDescription.getFileVOS().forEach(fileInfo->{
            AmArchive amArchive = new AmArchive();
            amArchive.setUrl(fileInfo.getUrl());
            amArchive.setTitle(fileInfo.getFileName());
            amArchive.setSubTitle(fileInfo.getFileName());
            //生成订单编号
            String billCode = UUID.randomUUID().toString().replaceAll("-", "");//serialService.generateBillCodeByBillType(BillTypeConstants.ARCHIVE_UPLOAD);
            amArchive.setCreateTime(DateUtils.getNowDate());
            amArchive.setCode(billCode);
            amArchive.setCreateBy(SecurityUtils.getUsername());
            amArchive.setDeptId(SecurityUtils.getDeptId());
            amArchive.setUserId(SecurityUtils.getUserId());
            //租户ID
            //amArchive.setTenantId(SecurityUtils.getTenantId());
            amArchive.setCreateTime(DateUtils.getNowDate());
            amArchive.setDescriptionId(amDescription.getId());
            amArchive.setDirId(fileInfo.getDirId());
            i.updateAndGet(v -> v + amArchiveMapper.insertAmArchive(amArchive));
        });

       return i.get();
    }
    /**
     * 修改档案文件
     * 
     * @param amArchive 档案文件
     * @return 结果
     */
    public int updateAmArchive(AmArchive amArchive)
    {
        amArchive.setUpdateTime(DateUtils.getNowDate());
        return amArchiveMapper.updateAmArchive(amArchive);
    }

    /**
     * 批量删除档案文件
     * 
     * @param ids 需要删除的档案文件主键
     * @return 结果
     */
    public int deleteAmArchiveByIds(Long[] ids)
    {
        return amArchiveMapper.deleteAmArchiveByIds(ids);
    }

    /**
     * 删除档案文件信息
     * 
     * @param id 档案文件主键
     * @return 结果
     */
    public int deleteAmArchiveById(Long id)
    {
        return amArchiveMapper.deleteAmArchiveById(id);
    }








    /****
     * 查询文件夹下的所有文件
     * @return
     */
    public Map queryFileByFolderId(Long id){
        //文件夹
        AmFolder queryFolder = new AmFolder();
        queryFolder.setParentId(id);
        queryFolder.setStatus(CommonConstants.ABLE);
        //queryFolder.setTenantId(SecurityUtils.getTenantId());
        List<AmFolder> folderList = amFolderMapper.selectAmFolderList(queryFolder);
        //文件
        AmArchive queryFile = new AmArchive();
        queryFile.setFolderId(id);
        queryFile.setStatus(CommonConstants.FLOW_STATUS_FINISH);
        queryFile.setAvailable(CommonConstants.ABLE);
        //queryFile.setTenantId(SecurityUtils.getTenantId());
        List<AmArchive> fileList = amArchiveMapper.selectAmArchiveList(queryFile);
        makeUrl(fileList);
        //自己
        AmFolder self = amFolderMapper.selectAmFolderById(id);
        Map<String,Object> data = new HashMap<>();
        data.put("folderList",folderList);
        data.put("fileList",fileList);
        data.put("self",self);
        return data;
    }


    /****
     * 查询档案柜下的所有文件
     * @return
     */
    public Map queryFileByCabinetId(Long id){
        //文件夹
        AmCabinet queryCabinet = new AmCabinet();
        queryCabinet.setParentId(id);
        List<AmCabinet> folderList = amCabinetMapper.selectAmCabinetList(queryCabinet);
        //文件
        AmArchive queryFile = new AmArchive();
        queryFile.setCabinetId(id);
        queryFile.setAvailable(CommonConstants.ABLE);
        List<AmArchive> fileList = amArchiveMapper.selectAmArchiveList(queryFile);
        makeUrl(fileList);
        //自己
        AmCabinet self = amCabinetMapper.selectAmCabinetById(id);
        Map<String,Object> data = new HashMap<>();
        data.put("folderList",folderList);
        data.put("fileList",fileList);
        data.put("self",self);
        return data;
    }

    /****
     * 不能将相对路径返回前端，防止直接通过url访问文件
     * @return
     */
    private void makeUrl(List<AmArchive> fileList){
        for(AmArchive file:fileList){
            String url = file.getUrl();
            String name = "";
            if(url.lastIndexOf("/")>0){
                name = url.substring(url.lastIndexOf("/")+1);
                file.setUrl(name);
            }
        }
    }

    public Boolean deleteAmArchiveByDescriptionIds(Long[] ids) {
        return amArchiveMapper.deleteAmArchiveByDescriptionIds(ids);
    }

    public Boolean deleteAmArchiveByDescriptionId(Long id) {
        return amArchiveMapper.deleteAmArchiveByDescriptionId(id);
    }

    public List<FileVO> selectAmArchiveListByDescId(Long id) {
        return amArchiveMapper.selectAmArchiveListByDescId(id);
    }

    public List<FileVO> selectByDescIdAndDirIds(Long id, List<Long> dirIds) {
        return amArchiveMapper.selectByDescIdAndDirIds(id, dirIds);
    }
}
